<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SwitchController
 *
 * @author Admin
 */
class Planning_SwitchController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function preDispatch() {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('/user/login');
        }

        $view = new Zend_View();
        $view->headTitle(' > Planning > Switch');
        $this->view->userDetail = $auth->getIdentity();
    }

    public function indexAction() {
        $switchModel = new Application_Model_SwitchModel();
        $paginator = $switchModel->getAllSwitches($this->_request);
        $this->view->paginator = $paginator;
    }

    public function listAction() {
        $this->_helper->layout->disableLayout();
        $switchModel = new Application_Model_SwitchModel();
        $paginator = $switchModel->getAllSwitches($this->_request);
        $this->view->paginator = $paginator;
    }

    public function addAction() {
        $this->_helper->layout->disableLayout();
        $switchModel = new Application_Model_SwitchModel();
        //create form
        $form = new Application_Form_switch_AddForm();

        // if request is post
        if ($this->_request->isPost()) {

            // if form is valid

            if ($form->isValid($this->_request->getPost())) {

                $data = array(
                    'name' => $this->_request->getPost('switchName'),
                    'cabinet' => $this->_request->getPost('cabinetList'),
                    'status' => $this->_request->getPost('status'),
                    'odf' => $this->_request->getPost("odf"),
                    'port' => $this->_request->getPost("port"),
                    'datum_patch' => $this->_request->getPost("datum_patch"),
                    'tomtprojektering' => $this->_request->getPost("tomtprojektering"),
                    'anmarkning_tomt' => $this->_request->getPost("anmarkning_tomt"),
                    'gravd_datum' => $this->_request->getPost("gravd_datum"),
                    'gravd_av' => $this->_request->getPost("gravd_av"),
                    'gravd_meter' => $this->_request->getPost("gravd_meter"),
                    'gravd_timmar' => $this->_request->getPost("gravd_timmar"),
                    'kopplad' => $this->_request->getPost("kopplad"),
                    'vagschakt_projekterad' => $this->_request->getPost("vagschakt_projekterad"),
                    'vagschakt_avropad' => $this->_request->getPost("vagschakt_avropad"),
                    'vagschakt_klar' => $this->_request->getPost("vagschakt_klar"),
                );
                $switchModel->save($data);

                //   $this->_redirect("planning/switch");
                //   exit;
            }
        }

        // assign form variable to view
        $this->view->form = $form;
    }

    public function editAction() {
        $this->_helper->layout->disableLayout();
        $switchModel = new Application_Model_SwitchModel();

        $switchId = $this->_request->getParam('id');
        $switchDetail = $switchModel->getSwitch('id', $switchId);
        //create form
        $form = new Application_Form_switch_AddForm();

        $form->populate(array(
            'switchName' => $switchDetail['name'],
            'cabinetList' => $switchDetail['cabinet'],
            'status' => $switchDetail['status'],
            'odf' => $switchDetail["odf"],
            'port' => $switchDetail["port"],
            'datum_patch' => $switchDetail["datum_patch"],
            'tomtprojektering' => $switchDetail["tomtprojektering"],
            'anmarkning_tomt' => $switchDetail["anmarkning_tomt"],
            'gravd_datum' => $switchDetail["gravd_datum"],
            'gravd_av' => $switchDetail["gravd_av"],
            'gravd_meter' => $switchDetail["gravd_meter"],
            'gravd_timmar' => $switchDetail["gravd_timmar"],
            'kopplad' => $switchDetail["kopplad"],
            'vagschakt_projekterad' => $switchDetail["vagschakt_projekterad"],
            'vagschakt_avropad' => $switchDetail["vagschakt_avropad"],
            'vagschakt_klar' => $switchDetail["vagschakt_klar"],
        ));
        // if request is post
        if ($this->_request->isPost()) {

            // if form is valid

            if ($form->isValid($this->_request->getPost())) {

                $data = array(
                    'id' => $switchId,
                    'name' => $this->_request->getPost('switchName'),
                    'cabinet' => $this->_request->getPost('cabinetList'),
                    'status' => $this->_request->getPost('status'),
                    'odf' => $this->_request->getPost("odf"),
                    'port' => $this->_request->getPost("port"),
                    'datum_patch' => $this->_request->getPost("datum_patch"),
                    'tomtprojektering' => $this->_request->getPost("tomtprojektering"),
                    'anmarkning_tomt' => $this->_request->getPost("anmarkning_tomt"),
                    'gravd_datum' => $this->_request->getPost("gravd_datum"),
                    'gravd_av' => $this->_request->getPost("gravd_av"),
                    'gravd_meter' => $this->_request->getPost("gravd_meter"),
                    'gravd_timmar' => $this->_request->getPost("gravd_timmar"),
                    'kopplad' => $this->_request->getPost("kopplad"),
                    'vagschakt_projekterad' => $this->_request->getPost("vagschakt_projekterad"),
                    'vagschakt_avropad' => $this->_request->getPost("vagschakt_avropad"),
                    'vagschakt_klar' => $this->_request->getPost("vagschakt_klar"),
                );
                $switchModel->save($data);

                //      $this->_redirect("planning/switch");
                //      exit;
            }
        }

        // assign form variable to view
        $this->view->form = $form;
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $switchId = $this->_request->getParam('id');
        $switchModel = new Application_Model_SwitchModel();

        $switchModel->delete("id = '$switchId'");
        //       $this->_redirect('planning/switch');
    }

    public function switchdetailAction() {
        $this->_helper->layout->disableLayout();

        $switchId = $this->_request->getParam('id');
        $documentModel = new Application_Model_DocumentModel();
        $form = new Application_Form_switch_CompletionForm();
        $switchModel = new Application_Model_SwitchModel();

        $switchDetail = $switchModel->getSwitch('id', $switchId);
        $files = $documentModel->getAllDocument('switch', $switchId);
        $form->populate(array(
            'complete' => $switchDetail['planning_percent_done']
        ));
        $this->view->switchDetail = $switchDetail;
        $this->view->files = $files;
        $this->view->form = $form;
    }

    public function updateprogressAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $switchId = $this->_request->getParam('id');
        $switchModel = new Application_Model_SwitchModel();
        $data = array(
            'id' => $switchId,
            'planning_percent_done' => $this->_request->getPost('complete')
        );
        $switchModel->save($data);
    }

    public function toggleAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $switchId = $this->_request->getParam('id');
        $status = $this->_request->getPost('status');
        $col = $this->_request->getPost('column');

        $switchModel = new Application_Model_SwitchModel();
        $switchDetail = $switchModel->getSwitch('id', $switchId);
        $percentdone = ($switchDetail[$col] == '0') ? ($switchDetail['planning_percent_done'] + 20) : ($switchDetail['planning_percent_done'] - 20);
        $data = array(
            'id' => $switchId,
            'planning_percent_done' => $percentdone,
            "$col" => ($switchDetail[$col] == '0') ? '1' : '0'
        );
        $switchModel->save($data);
    }

    public function uploadprogressfileAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        // $form = new Application_Form_documents_AddForm();
        $switchId = $this->_request->getParam('id');
        $switch_progress_name = $this->_request->getParam('page');

        if ($_FILES) {
            $uploaded = move_uploaded_file($_FILES["doc"]["tmp_name"], "./uploads/files/" . $_FILES["doc"]["name"]);

            if ($uploaded) {
                $data = array(
                    'name' => $_FILES["doc"]["name"],
                    'uploader_id' => $this->view->userDetail->id,
                    'file' => $_FILES["doc"]["name"],
                    'switch' => $switchId,
                    'status' => 'active',
                    'switch_progress_name' => $switch_progress_name,
                    'date' => Zend_Date::now()->toString('yyyy-MM-dd HH:mm:ss')
                );
                $documentModel = new Application_Model_DocumentModel();
                $documentModel->save($data);
                $this->_redirect("planning/switch");
                // exit;
            }
        }
        $this->_redirect("planning/switch");
    }

    public function deletefileAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        // $form = new Application_Form_documents_AddForm();
        $fileId = $this->_request->getParam('id');

        $documentModel = new Application_Model_DocumentModel();
        $documentModel->delete("id = '$fileId'");
    }

}